// 标题页
//  MZY_TitleViewController.m
//  FullHouse
//
//  Created by dllo on 16/1/16.
//  Copyright © 2016年 yy. All rights reserved.
//

#import "MZY_TitleViewController.h"
#import "NetworkingTool.h"
#import "UIImageView+WebCache.h"
#import "MZY_ButttonModel.h"
#import "MZY_ButtonCell.h"
#import "MBProgressHUD.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface MZY_TitleViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain)UITableView *textTableView;
@property(nonatomic, retain)UITableView *pictureTableView;
@property(nonatomic, retain)NSMutableArray *textArr;
@property(nonatomic, retain)NSMutableArray *pictureArr;
@property(nonatomic, retain)MBProgressHUD *hud;

@end

@implementation MZY_TitleViewController

- (void)dealloc
{
    [self.textTableView release];
    [self.pictureTableView release];
    [self.pictureArr release];
    [self.textArr release];
    [self.hud release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createView];
    [self createData];
}

#pragma mark 获取数据
- (void)createData{
    [NetworkingTool networkingWithStrURL:@"http://api.meilijia.com/app/idea_photos_tags2" type:GETType bodyStr:nil block:^(id result) {
        self.textArr = [NSMutableArray array];
        for (NSDictionary *dic in result[@"data"][@"idea_photos_tags2"][@"tags"]) {
            MZY_ButttonModel *button = [[MZY_ButttonModel alloc]init];
            [button setValuesForKeysWithDictionary:dic];
            [self.textArr addObject:button];
            
            self.pictureArr = [NSMutableArray array];
            for (NSDictionary *temp in dic[@"tags"]) {
                MZY_ButttonModel *picButton = [[MZY_ButttonModel alloc]init];
                [picButton setValuesForKeysWithDictionary:temp];
                [self.pictureArr addObject:picButton];
                [self.textTableView reloadData];
                [self.pictureTableView reloadData];
            }
            [button setValue:self.pictureArr forKey:@"tags"];
            self.hud.hidden = YES;
        }
    }];
}

#pragma mark 创建界面
- (void)createView{
    
    self.view.backgroundColor = [UIColor cyanColor];
    
    // 返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 20, 20);
    [backButton setImage:[UIImage imageNamed:@"iconfont-arrowleft.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.textTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 3, HEIGHT - 64) style:UITableViewStylePlain];
    self.textTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.textTableView];
    self.textTableView.delegate = self;
    self.textTableView.dataSource = self;
    [self.textTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"text"];
    [self.textTableView release];
    
    self.pictureTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH / 3, 0, WIDTH / 3 * 2, HEIGHT - 64) style:UITableViewStylePlain];
    self.pictureTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.pictureTableView];
    self.pictureTableView.delegate = self;
    self.pictureTableView.dataSource = self;
    [self.pictureTableView registerClass:[MZY_ButtonCell class] forCellReuseIdentifier:@"picture"];
    [self.pictureTableView release];
    
    // 加载动画
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];

}

#pragma mark "tableView"的第一个必须执行的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.textTableView == tableView) {
        return self.textArr.count;
    }else{
        return self.pictureArr.count;
    }
}

#pragma mark "tableView"的第二个必须执行的方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.textTableView == tableView) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"text" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellEditingStyleNone;
        MZY_ButttonModel *button = self.textArr[indexPath.row];
        cell.textLabel.text = button.Class;
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        return cell;
    }else{
        MZY_ButtonCell *cell = [tableView dequeueReusableCellWithIdentifier:@"picture" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellEditingStyleNone;
        MZY_ButttonModel *button = self.pictureArr[indexPath.row];
        cell.titleLabel.text = button.value;
        [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:button.pic]];
        return cell;
    }
}

#pragma mark "tableView"的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.textTableView == tableView) {
        MZY_ButttonModel *button = self.textArr[indexPath.row];
        self.pictureArr = button.tags;
        [self.pictureTableView reloadData];
    }else{
        MZY_ButttonModel *but = self.pictureArr[indexPath.row];
        [self.navigationController popToRootViewControllerAnimated:YES];
        // 调用"block"
        self.block(but.value);
        
        [self.delegate bringStr:but.query];
        
    }
}

#pragma mark 返回按钮的点击方法
- (void)backAction:(UIButton *)button{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
